# -*- coding: UTF-8 -*-
""" Shows the results of previous game

    From GameState It needs a pair of players and list of words they guessed.
    It also needs current_word if it was time-outed, or None
    It can change phase to the screen "from -> to"
    furthermore, here player can delete words, if pair failed it,
    but clicked, that they guessed it
"""

from kivy.clock import Clock
from kivy.core.audio import SoundLoader

from ui.fittable_widgets import FittableButton, FittableLabel
from kivy.properties import StringProperty
from kivy.properties import NumericProperty
from model.gamestate import *
from model.gamestate import Phase
from ui.hat_widget import HatWidget


class WordLabel(FittableButton):
    colors = {WordGuessResult.FAIL: [1, 0, 0, 1],
              WordGuessResult.GUESS: [0, 1, 0, 1],
              WordGuessResult.TIME_OUT: [1, 1, 1, 1],
              WordGuessResult.PUT_BACK: [1, 1, 1, 1]}
    ORDER = [WordGuessResult.GUESS,
             WordGuessResult.FAIL,
             WordGuessResult.PUT_BACK]

    def __init__(self, guess_result, **kwargs):
        self.guess_result = guess_result
        self.color = self.colors[guess_result.result]
        self.word = guess_result
        super(WordLabel, self).__init__(**kwargs)

    #def on_touch_up(self, touch):
        #if self.collide_point(touch.x, touch.y):
            #self.swap_result()

    def swap_result(self):
        word = self.guess_result
        order = self.ORDER
        if word.result == WordGuessResult.TIME_OUT:
            word.result = WordGuessResult.PUT_BACK
        word.result = order[(order.index(word.result) + 1) % len(order)]

        self.color = self.colors[word.result]
        self.texture_update()


class ResultsOfOnePair(HatWidget):
    next_phase = NumericProperty()

    def __init__(self, gamestate, presenter, **kwargs):
        """ Sets up the screen.
        """
        self.button_text = {Phase.GAME_RESULTS: _('Game results'),
                            Phase.FROM_TO: _('Pass the hat')}

        self.words_left = len(gamestate.hat.words)

        self.next_phase = Phase.FROM_TO

        super(ResultsOfOnePair, self).__init__(**kwargs)
        self.presenter = presenter
        self.gamestate = gamestate
        self.word_list.bind(minimum_height=self.word_list.setter('height'))

        for word in self.gamestate.game_round.words:
            if (word.result in [WordGuessResult.FAIL, WordGuessResult.GUESS,
                                WordGuessResult.TIME_OUT]):
                self.word_list.add_widget(WordLabel(word))
        if not self.gamestate.hat.words:
            self.next_phase = Phase.GAME_RESULTS

    def to_next(self, is_finishing=False):
        """ changes phase to 'FROM_TO' or 'GAME_RESULTS'
        called on 'Следующий раунд' button release
        """
        for guess in self.gamestate.game_round.words[:]:
            if guess.result in [WordGuessResult.PUT_BACK,
                                WordGuessResult.TIME_OUT]:
                self.gamestate.hat.add_word(guess.word)

        if self.gamestate.rounds is None:
            self.gamestate.rounds = []
        self.gamestate.rounds.append(self.gamestate.game_round)
        self.gamestate.game_round = None
        if is_finishing:
            self.presenter.change_phase(Phase.CURRES)
        else:
            self.presenter.change_phase(self.next_phase)
